package com.cts.rt.dao.hibernate;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.cts.rt.dao.EmployeeLevelDao;
import com.cts.rt.model.EmployeeLevel;

/**
 * This class interacts with hibernate session to save/delete and retrieve Role
 * objects.
 * 
 * @author <a href="mailto:bwnoll@gmail.com">Bryan Noll</a>
 * @author jgarcia (updated to hibernate 4)
 */
@Repository
public class EmployeeLevelDaoHibernate extends
		GenericDaoHibernate<EmployeeLevel, Long> implements EmployeeLevelDao {

	/**
	 * Constructor to create a Generics-based version using Role as the entity
	 */
	public EmployeeLevelDaoHibernate() {
		super(EmployeeLevel.class);
	}

	/**
	 * {@inheritDoc}
	 */
	public EmployeeLevel getLevelByName(String rolename) {
		List roles = getSession().createCriteria(EmployeeLevel.class)
				.add(Restrictions.eq("name", rolename)).list();
		if (roles.isEmpty()) {
			return null;
		} else {
			return (EmployeeLevel) roles.get(0);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public void removeEmployeeLevel(String rolename) {
		Object role = getLevelByName(rolename);
		Session session = getSessionFactory().getCurrentSession();
		session.delete(role);
	}
}
